package com.beiding.dbrequest.server;

import com.beiding.dbrequest.common.Common;
import com.beiding.dbrequest.common.Mapping;
import com.beiding.dbrequest.common.Request;
import com.beiding.dbrequest.utils.QueryUtils;
import lombok.Getter;
import lombok.extern.java.Log;

import java.io.Serializable;
import java.util.Map;

/**
 * 注册用服务端控制器
 * 提供简单的注册处理逻辑,非必须
 */
@Log
public class RegisterServerController {

    @Getter
    private ChannelRegisterTable channelRegisterTable = new ChannelRegisterTable();


    /**
     * @param request
     * @return
     */
    @Mapping("/register")
    public Serializable register(Request request) {
        String text = (String) request.getBody();
        if (text != null) {
            Map<String, String> metadata = QueryUtils.getQuery(text);
            String name = QueryUtils.getPath(text);
            channelRegisterTable.register(request.getCtx(), name, metadata);
        } else {
            throw new IllegalArgumentException("注册服务名称不能为空");
        }
        return request.getCtx().channel().id().asShortText().getBytes(Common.CHARSET);
    }
}
